import { Switch, Redirect, Route } from 'react-router-dom';

const RouterView = ({ routerList }) => {
    if (!routerList) {
        return;
    }

    let routes = routerList.filter(item => item.path);
    let redirect = routerList.filter(item => item.to)[0];

    return (
        <Switch>
            {
                routes && routes.map((item, index) => (
                    <Route key={index} path={item.path} render={props => {
                        let Com = item.component;

                        //获取token
                        let token = window.sessionStorage.getItem("token");

                        if (item.requiresAuth && !token) {
                            return <Redirect to="/" from={item.path} />
                        }
                        if (item.children) {
                            return <Com {...props} routerList={item.children} />
                        }
                        return <Com {...props} />
                    }} />
                ))
            }
            {
                redirect && <Redirect to={redirect.to} from={redirect.from} />
            }
        </Switch>
    )
};

export default RouterView;